import numpy
import sys,os,inspect
from PyQt4 import QtCore
from KModel.KSingleStock import KSingleStock
from KModel.KSingleStockDataProcessor import KSingleStockDataProcessor as kprocesser
from KModel.KSingleStockCursor import KSingleStockCursor 
import pandas as pd
from datetime import datetime;
#sys.path.insert(0,os.path.abspath(os.path.join(os.getcwd(), os.pardir)))## add root path 
from KTrade.KTimeBar import KTimeBar
class KStockFeed(QtCore.QObject):
	''' the brother of KSingleStock(KSingleStock is the general purpose one)
	is in terms of stream, desgined for simulation in strategy test
	It should be created by passing KSingleStock to it	
	'''
	
	def __init__(self,parent=None): # parent must be a KSingleStock
		QtCore.QObject.__init__(self,parent)
		assert(isinstance(parent,KSingleStock)==True)
		self.m_stock=parent
		mstock=self.m_stock
		self.period=mstock.GetCurPeriod() # record how many days per bar
		self.nparray=mstock.GetNumpyArray() # get all data in terms of current period
		
			
		self.curbarindex=0 # the starting row for simulation
	
	def GetStockSymbol(self):
		return self.m_stock
	
	def GetSymbolName(self):
		return self.m_stock.GetSymbolName()
	

	def TotalBarNum(self):
		return len(self.nparray)
	
	def HasNextBar(self):
		return not self.curbarindex==(self.TotalBarNum()-1)
	
	def GetCurrentIndex(self):
		return self.curbarindex
	
	def MoveForward(self):
		self.curbarindex+=1



	def GetAllBars(self,dimname='All'):
		if dimname=='All':
			return self.nparray[:,:]
		else:
			dimindex=KSingleStock.GetDimensionIndexByName(dimname)
			return self.nparray[:,dimindex]		
	
	def GetBarByIndex(self,index,dimname='All'):
		if dimname=='All':
			return self.nparray[index,:]
		else:
			dimindex=KSingleStock.GetDimensionIndexByName(dimname)
			return self.nparray[index,dimindex]	
		

	
	def GetBarWindowByIndex(self,dimname,indexrange):
		dimindex=KSingleStock.GetDimensionIndexByName(dimname)		
		return self.nparray[indexrange[0]:indexrange[1],dimindex]
	
	def GetBarsTillNow(self,dimname='All'):
		# all bars till current bar
		if dimname=='All':
			return self.nparray[0:self.curbarindex+1,:]
		else:
			dimindex=KSingleStock.GetDimensionIndexByName(dimname)
			return self.nparray[0:self.curbarindex+1,dimindex]		

		

		

	